package com.fmi.chesstern.strategy;

import java.util.ArrayList;

import com.fmi.chesstern.gameboard.GameBoard;
import com.fmi.chesstern.gameboard.GameField;
import com.fmi.chesstern.pieces.ChessPiece;
import com.fmi.chesstern.pieces.ChessPiece.PieceColor;

public class RookStrategy implements IGameStrategy {
	
	public ArrayList<GameField> getValidMoves(GameField currentPosition, GameBoard board) {
		ArrayList<GameField> allMoves = new ArrayList<GameField>();
		PieceColor color = board.getPieces().get(currentPosition.toString()).getColor();
		for (int k = currentPosition.getRow() + 1; k < 8; k++) {
			GameField field = new GameField(k, currentPosition.getColumn());
			ChessPiece piece = board.getPieces().get(field.toString());
			if (piece != null) {
				if (piece.getColor() != color) {
					allMoves.add(field);
					break;
				} else {
					break;
				}
			}
			allMoves.add(field);
		}
		
		for (int k = currentPosition.getRow() - 1; k >= 0; k--) {
			GameField field = new GameField(k, currentPosition.getColumn());
			ChessPiece piece = board.getPieces().get(field.toString());
			if (piece != null) {
				if (piece.getColor() != color) {
					allMoves.add(field);
					break;
				} else {
					break;
				}
			}
			allMoves.add(field);
		}
		
		for (int l = currentPosition.getColumn() + 1; l < 8; l++) {
			GameField field = new GameField(currentPosition.getRow(), l);
			ChessPiece piece = board.getPieces().get(field.toString());
			if (piece != null) {
				if (piece.getColor() != color) {
					allMoves.add(field);
					break;
				} else {
					break;
				}
			}
			allMoves.add(field);
		}
		
		for (int l = currentPosition.getColumn() - 1; l >= 0; l--) {
			GameField field = new GameField(currentPosition.getRow(), l);
			ChessPiece piece = board.getPieces().get(field.toString());
			if (piece != null) {
				if (piece.getColor() != color) {
					allMoves.add(field);
					break;
				} else {
					break;
				}
			}
			allMoves.add(field);
		}
		
		ArrayList<GameField> moves = new ArrayList<GameField>();
		for (GameField move : allMoves) {
			if (GameField.isFieldCorrect(move)) {
				moves.add(move);
			}
		}
		return moves;
	}

}
